package com.example.manualManagement.service.impl.user.account;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.manualManagement.entity.Manage;
import com.example.manualManagement.mapper.ManageMapper;
import com.example.manualManagement.service.user.account.ModiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class ModiServiceImpl implements ModiService {





    @Autowired
    private ManageMapper manageMapper;

    @Override
    public Map<String, String> modify(String userName, String userPassword, String newPassword) {
        Map<String, String> map = new HashMap<>();
        if(userPassword.equals(newPassword)){
            map.put("error_massage", "修改的密码不能和原密码相同");
            return map;
        }

        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("userName", userName);
        Manage manage = manageMapper.selectOne(queryWrapper);


        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();

        String sqlPassword = manage.getUserpassword();
        if(bCryptPasswordEncoder.matches(userPassword, sqlPassword)){
            String encodedNewPassword = bCryptPasswordEncoder.encode(newPassword);
            manage.setUserpassword(encodedNewPassword);
            manageMapper.updateById(manage);
            map.put("error_massage", "modifysuccessfully");
            return map;
        }else{
            map.put("error_massage", "error");
            return map;
        }
    }
}
